﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using DM4;
using sscore;
using System.Linq;
using sscore.Utils;
using System.Text.RegularExpressions;
using DevExpress.XtraEditors;

namespace DM4Client.AddEditForms
{
    public partial class FrmAddEditMailRule : XtraForm
    {
        public int Id = DM4Consts.DefaultNULLId;
        public OperationResult Result = new OperationResult();
        public const string FrmCaption = "New mail rule adding";

        public FrmAddEditMailRule(int id = DM4Consts.DefaultNULLId)
        {
            InitializeComponent();
            Id = id;
        }

        private void FrmAddEditMailRule_Shown(object sender, EventArgs e)
        {
            foreach (string str in MailRuleStrings.Dict.Values)
                if (!String.IsNullOrEmpty(str))
                    cbRuleType.Properties.Items.Add(str);


            if (Id == DM4Consts.DefaultNULLId)
            {
                Text = FrmCaption;
            }
            else
            {
                DataRow[] drcoll = FrmMain.DtMailRules.Select(String.Format("[{0}]={1}", CnMailRules.Id, Id));
                if (drcoll.Length > 0)
                {
                    DataRow dr = drcoll[0];
                    Text = String.Format("Edit mail rule: {0}", dr[CnMailRules.RuleName]);
                    tbRuleName.Text = dr[CnMailRules.RuleName].ToString();
                    tbRule.Text = dr[CnMailRules.Rule].ToString();

                    string ruleType = SysUtils.ConvertFromDBVal<string>(dr[CnMailRules.RuleType]);
                    MailRuleType mrt = MailRuleValues.Dict.First(kvp => kvp.Value.Equals(ruleType)).Key;

                    cbRuleType.SelectedIndex = cbRuleType.Properties.Items.IndexOf(MailRuleStrings.Dict[mrt]);
                }
                else
                {
                    Text = FrmCaption;
                }
            }
            tbRuleName.Focus();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                if (String.IsNullOrEmpty(tbRuleName.Text))
                {
                    MessageBox.Show(this, "Name field is empty", DmConsts.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    tbRuleName.Focus();
                    return;
                }

                if (String.IsNullOrEmpty(tbRule.Text))
                {
                    MessageBox.Show(this, "Rule field is empty", DmConsts.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    tbRule.Focus();
                    return;
                }
                else
                {
                    try
                    {
                        new Regex(tbRule.Text);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(this, "Rule field value invalid for Regex creating."
                            + Environment.NewLine + ex.Message, DmConsts.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        tbRule.Focus();
                        return;
                    }
                }

                if (String.IsNullOrEmpty(cbRuleType.Text))
                {
                    MessageBox.Show(this, "Type field is empty", DmConsts.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    cbRuleType.Focus();
                    return;
                }

                if (Id == DM4Consts.DefaultNULLId)
                {
                    Result = FrmMain.AddMailRule(
                        tbRuleName.Text.Trim()
                        , tbRule.Text.Trim()
                        , MailRuleStrings.Dict.First(kvp => kvp.Value.Equals(cbRuleType.Text)).Key);

                }
                else
                {
                    Result = FrmMain.EditMailRule(Id
                        , tbRuleName.Text.Trim()
                        , tbRule.Text.Trim()
                        , MailRuleStrings.Dict.First(kvp => kvp.Value.Equals(cbRuleType.Text)).Key);
                }

                if (Result.Result > 0)
                {
                    DialogResult = DialogResult.OK;
                    Close();
                }
                else
                {
                    MessageBox.Show(this, Result.Message, DmConsts.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                string m = Id == DM4Consts.DefaultNULLId
                    ? "Error of mail rule creating"
                    : "Error of mail rule editing";
                FrmMain.WriteError(m + Environment.NewLine + ex.Message);
                Close();
            }
        }
    }
}